package com.example.meituan.activity.user.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.meituan.R;
import com.example.meituan.activity.bean.AdminBean;
import com.example.meituan.activity.bean.BusinessBean;
import com.example.meituan.activity.bean.TalkBean;
import com.example.meituan.activity.user.OnMessageItemClickListener;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.TalkDao;


import java.io.File;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{

    private List<TalkBean> messageList;
    private static OnMessageItemClickListener listener;
    private  static int ifUser;


    public MessageAdapter(List<TalkBean> messageList, OnMessageItemClickListener listener,int ifUser) {
        this.messageList =messageList;
        this.listener=listener;
        this.ifUser=ifUser;
    }


    @NonNull
    @Override
    public MessageAdapter.MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list, parent, false);
        return new MessageAdapter.MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageAdapter.MessageViewHolder holder, int position) {
        TalkBean talkBean = messageList.get(position);
        holder.bind(talkBean);
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }

    public static class MessageViewHolder extends RecyclerView.ViewHolder {

        private TextView messageName, messageTalk, messageTime;
        private ImageView messageImg;

        public MessageViewHolder(@NonNull View itemView) {
            super(itemView);
            messageName = itemView.findViewById(R.id.message_name);
            messageImg = itemView.findViewById(R.id.message_img);
            messageTalk = itemView.findViewById(R.id.message_talk);
            messageTime = itemView.findViewById(R.id.message_time);
        }

        public void bind(TalkBean talkBean) {
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.toMessageView(talkBean);
                }
            });
            if(ifUser==1){
                BusinessBean businessBean = AdminDao.getBusiness(talkBean.getShopId());
                messageName.setText(businessBean.getsName());
                String imagePath = businessBean.getsImg();
                File imageFile = new File(imagePath);
                if (imageFile.exists()) {
                    Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                    if (bitmap != null) {
                        messageImg.setImageBitmap(bitmap);
                    }
                }
                AdminBean currentUser=AdminDao.getUserInfo(itemView.getContext());
                TalkBean talkBean1=TalkDao.getTalkByUserIdLeast(currentUser.getUserId());
                messageTalk.setText( talkBean1.getTalk());
                Instant instant = Instant.ofEpochMilli(talkBean1.getTime());
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd")
                        .withZone(ZoneId.systemDefault());
                messageTime.setText(formatter.format(instant));
        }
    }


        }

    }

